<?php


namespace app\service;


use app\model\Result;
use think\facade\Db;

class OrderService
{

    /**
     * 根据订单uuid获取订单详情
     */
    public static function queryOrderByUuid($param) {

        $res = Db::name('order')
            ->where('order.uuid','=',$param['uuid'])
            ->alias("order")
            ->join('user user', 'order.user_uuid = user.uuid')
            ->join('goods goods', 'order.goods_uuid = goods.uuid')
            ->field('order.*,goods.goods,goods.price,goods.img,user.name')
            ->select();

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 新增订单
     */
    public static function createOrder($param) {
        $id = Db::name('order')->insertGetId($param);

        if (empty($id)) {
            return Result::index(100,'插入失败','');
        }

        return Result::index(200,'插入成功','');
    }

    /**
     * 订单付款
     */
    public static function payOrder($param) {

        Db::startTrans();
        try{
            $res = Db::name('order')->where('uuid','=',$param['uuid'])->update([
                'pay_time' => $param['pay_time'],
                'status' => 2
            ]);

            $order = Db::name('order')
                ->where('order.uuid','=',$param['uuid'])
                ->alias("order")
                ->join('user user', 'order.user_uuid = user.uuid')
                ->join('goods goods', 'order.goods_uuid = goods.uuid')
                ->field('order.*,goods.pay_num,user.buy_num')
                ->find();

            Db::name('user')->where('uuid','=',$order['user_uuid'])->update(['buy_num'=>$order['buy_num']+1]);
            Db::name('goods')->where('uuid','=',$order['goods_uuid'])->update(['pay_num'=>$order['pay_num']+1]);

            Db::commit();
            return Result::index(200,'付款成功',$res);

        } catch (\Exception $e) {
            Db::rollback();
            return Result::index(100,'付款失败',$e);
        }
    }

    /**
     * 订单发货
     */
    public static function deliverOrder($param) {

        $res = Db::name('order')->where('uuid','=',$param['uuid'])->update([
            'deliver_time' => $param['deliver_time'],
            'status' => 3
        ]);

        return Result::index(200,'发货成功',$res);
    }

    /**
     * 订单收货
     */
    public static function receiveOrder($param) {

        $res = Db::name('order')->where('uuid','=',$param['uuid'])->update([
            'receive_time' => $param['receive_time'],
            'status' => 4
        ]);

        return Result::index(200,'收货成功',$res);
    }

    /**
     * 订单取消
     */
    public static function deleOrder($param) {

        $res = Db::name('order')->where('uuid','=',$param['uuid'])->update([
            'dele_time' => $param['dele_time'],
            'dele_reason' => $param['dele_reason'],
            'status' => 0
        ]);

        return Result::index(200,'取消成功',$res);
    }

    /**
     * 返回对应状态对应类型全部订单列表（由新到旧）
     */
    public static function listAllOrder($param) {

        $res = Db::name('order')
            ->where('order.status',$param['status'])
            ->where('order.type',$param['type'])
            ->alias("order")
            ->join('user user', 'order.user_uuid = user.uuid')
            ->join('goods goods', 'order.goods_uuid = goods.uuid')
            ->field('order.*,goods.goods,goods.price,goods.img,user.name')
            ->order('create_time desc')
            ->limit($param['startPage'],$param['endPage'])
            ->select();

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 返回对应状态对应类型全部个人订单列表（由新到旧）
     */
    public static function listAllUserOrder($param) {

        $res = Db::name('order')
            ->where('order.user_uuid',$param['user_uuid'])
            ->where('order.status',$param['status'])
            ->where('order.type',$param['type'])
            ->alias("order")
            ->join('user user', 'order.user_uuid = user.uuid')
            ->join('goods goods', 'order.goods_uuid = goods.uuid')
            ->field('order.*,goods.goods,goods.price,goods.img,user.name')
            ->order('create_time desc')
            ->limit($param['startPage'],$param['endPage'])
            ->select();

        return Result::index(200,'操作成功',$res);
    }
}
